<template >
	<view  class="page">
    <view >
      <img style="height: 250px;width: 100vw; border-radius: 20px;" :src="list.img" alt="">
      <view style="margin-top: 10px;">商品类别：{{ list.className }}</view>
      <view style="margin-top: 10px;">商品名称：{{ list.name }}</view>
      <view style="margin-top: 10px;">商品单价：{{ list.price }}积分</view>
      <view style="margin-top: 10px;">商品数量:{{ list.count }}件</view>
      <view style="margin-top: 10px;margin-bottom: 10px;">商品总价：{{ list.count * list.price}}积分</view>
      <u--form ref="uForm" label-position="left" :model="form" :rules="rules">
        <u-form-item required label="收货人姓名" prop="ordername" border-bottom>
					<u--input v-model="form.ordername" border="none" placeholder="收货人姓名"></u--input>
				</u-form-item>
        <u-form-item required label="电话" prop="phone" border-bottom>
					<u--input v-model="form.phone" border="none" placeholder="请输入联系电话"></u--input>
				</u-form-item>
        <u-form-item required label="地址" prop="address" border-bottom>
					<u--textarea v-model="form.address"  border="none" count placeholder="请输入家庭地址" ></u--textarea>
				</u-form-item>
      </u--form>
      <!-- 收货人姓名：<u--input style="margin-top: 10px;margin-bottom: 10px;" v-model="ordername" border="none" placeholder="请输入昵称"></u--input>
      收货人电话：<u--input style="margin-top: 10px;margin-bottom: 10px;" v-model="phone" border="none" placeholder="请输入昵称"></u--input>
      收货地址：<u--textarea style="margin-top: 10px;" v-model="address" placeholder="请输入地址" ></u--textarea> -->

      <u-button style="width: 50%;margin-top: 10px;" shape="circle" @click="makeSure" type="primary" icon="checkmark-circle" text="确认支付"></u-button>
      <u-modal :show="show" @cancel="cancel" @confirm="confirm" title="确认支付" content='该商品已经在订单里面了，确定继续支付？' :showCancelButton="true"></u-modal>
    </view>
	</view>
</template>

<script>
import {baseUrl} from '@/config'
export default {

	data() {
		return {
      doller:0,
      all:'',
      userId:0,
      form:{
          phone:'',
          ordername:'',
          address:'',
        },
      rules:{
        phone: [
            {
              required: true, 
              message: '请输入手机号',
              trigger: ['change','blur'],
            },
            {
              validator: (rule, value, callback) => {
                return uni.$u.test.mobile(value);
              },
              message: '手机号码不正确',
              trigger: ['change','blur'],
            }
          	],
            ordername: [
					{
						required: true,
						message: '请输入昵称',
            trigger: ['blur', 'change']
					}
				],
        address:[
            {
						required: true,
						message: '请输入地址',
            trigger: ['blur', 'change']
					}
            ]
        },
      show:false,
      id:0,
      list:[],
      count:1,
      path:'',
      orderList:[]
		};
	},
  onLoad(Option){
    this.id = Option.id  
    this.count = Option.count
    this.path = Option.path
},
	onShow() {
    this.userId=uni.getStorageSync('user').data[0].userId
    this.getProduct()
    this.getorderList()
	},
  onReady() {
		this.$refs.uForm.setRules(this.rules)
	},
	methods: {
    confirm(){
      this.userId=uni.getStorageSync('user').data[0].userId
      uni.request({
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              url: baseUrl+'/v1/order/shopOrderAdd', 
              data:{
                userId:this.userId,
                className:this.list.className,
                code:this.list.code,
                name:this.list.name,
                img:this.list.img,
                price:this.list.price,
                count:this.list.count,
                status:0,
                address:this.form.address,
                phone:this.form.phone,
                ordername:this.form.ordername
              },
              success:()=>{
                uni.showToast({
                      title: '支付完成',
                      duration: 1000
                  });
                setTimeout(function() {
                uni.navigateTo({
                  url:`/pages/shop-order/index`
              })
            }, 1500)
              }
        })

    },
    cancel(){
      this.show = false
    },
    makeSure(){
      this.userId=uni.getStorageSync('user').data[0].userId

      this.$refs.uForm.validate().then(res => {
        if (res) {
          this.getProduct()
          this.getorderList()
          if(this.orderList == undefined){
              uni.request({
                url:baseUrl+"/v1/order/shopOrderAdd",
                method: 'POST',
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                  },
              data: {
                className:this.list.className,
                code:this.list.code,
                userId:this.userId,
                name:this.list.name,
                img:this.list.img,
                price:this.list.price,
                count:this.list.count,
                status:0,
                address:this.form.address,
                phone:this.form.phone,
                ordername:this.form.ordername
              },
              success:()=>{
                uni.request({
                  method: 'GET',
                        url: baseUrl+'/v1/user/userIdSearch',
                        data:{
                          userId : this.userId,
                        },
                        success: (res) => {
                          this.yue = res.data.data[0].doller
                          this.all =  this.list.count * this.list.price
                          if(this.all > this.yue){
                            uni.showToast({
                                title: '余额不足，马上为你跳转支付页面',
                                duration: 1000
                              });
                              setTimeout(function() {
                                  uni.navigateTo({
                                          url:'/pages/doller/index'
                                        });
                                }, 1500)
                          }
                          else{
                            
                            this.dol = this.yue - this.all
                            uni.request({
                              method: 'POST',
                                    header: {
                                      'Content-Type': 'application/x-www-form-urlencoded'
                                    },
                                    url: baseUrl+'/v1/user/userUpdate',
                                    data:{
                                        id : uni.getStorageSync('user').data[0].id,
                                        doller : this.dol
                                    },
                                    success: (res) => {
                                      this.doller = res.data.date.doller
                                      uni.showToast({
                                                  title: '支付完成',
                                                  duration: 1000
                                              });
                                            setTimeout(function() {
                                            uni.navigateTo({
                                              url:`/pages/shop-order/index`
                                          })
                                        }, 1500)
                                      }
                                });

                          }
                        }
                      })
                    

          }
            })
          }else{
              for(let i=0;i<this.orderList.length;i++){
                if(this.orderList[i].id == this.list.id){
                  this.show = true

                return 
                }
            }
            uni.request({
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              url: baseUrl+'/v1/order/shopOrderAdd', 
              data:{
                className:this.list.className,
                code:this.list.code,
                // id:this.list.id,
                userId:this.userId,
                name:this.list.name,
                img:this.list.img,
                price:this.list.price,
                count:this.list.count,
                status:0,
                address:this.form.address,
                phone:this.form.phone,
                ordername:this.form.ordername
                
              },
              success:()=>{
                uni.request({
                  method: 'GET',
                        url: baseUrl+'/v1/user/userIdSearch',
                        data:{
                          userId : this.userId,
                        },
                        success: (res) => {
                          this.yue = res.data.data[0].doller
                          this.all =  this.list.count * this.list.price
                          if(this.all > this.yue){
                            uni.showToast({
                                title: '余额不足，马上为你跳转支付页面',
                                duration: 1000
                              });
                              setTimeout(function() {
                                  uni.navigateTo({
                                          url:'/pages/doller/index'
                                        });
                                }, 1500)
                          }
                          else{
                            
                            this.dol = this.yue - this.all
                            uni.request({
                              method: 'POST',
                                    header: {
                                      'Content-Type': 'application/x-www-form-urlencoded'
                                    },
                                    url: baseUrl+'/v1/user/userUpdate',
                                    data:{
                                        id : uni.getStorageSync('user').data[0].id,
                                        doller : this.dol
                                    },
                                    success: (res) => {
                                      this.doller = res.data.date.doller
                                      uni.showToast({
                                                  title: '支付完成',
                                                  duration: 1000
                                              });
                                            setTimeout(function() {
                                            uni.navigateTo({
                                              url:`/pages/shop-order/index`
                                          })
                                        }, 1500)
                                      }
                                });

                          }
                        }
                      })

              }
        })
          }
        }
        })
      },
    getProduct(){
      if(this.path == 'car'){
        uni.request({
            data: {
          id : this.id
            }, 
            url: baseUrl+'/v1/car/idSearch',
            success: (res) => {
              console.log(res);
              this.list = res.data.data[0]
              this.list.count = this.count
             
              }
        });
      }else{
        uni.request({
            data: {
          id : this.id
            }, 
            url: baseUrl+'/v1/product/idSearch',
            success: (res) => {
              this.list = res.data.data[0]
              this.list.count = 1
              // console.log(this.list);
              }
        });
      }


    },
    getorderList(){
      this.userId=uni.getStorageSync('user').data[0].userId
        uni.request({
              url: baseUrl+'/v1/order/shopOrderSearch',
              data:{
                userId:this.userId
              },
              success: (res) => {
                this.orderList = res.data.data
                }
          });
    }
	}
};
</script>

<style lang="scss" scoped>

</style>
